package com.Wlongtao.eBusiness.cotroller;


import com.Wlongtao.eBusiness.dao.GameRepository;
import com.Wlongtao.eBusiness.dao.PlayerRepository;
import com.Wlongtao.eBusiness.entity.Game;
import com.Wlongtao.eBusiness.entity.Player;
import com.Wlongtao.eBusiness.services.GameService;
import com.Wlongtao.eBusiness.services.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("games")
public class GameController {

    @Autowired
    GameService gameService;

    @Autowired
    PlayerService playerService;

    @GetMapping
    public String displayGames(Model model){

        List<Game> games = gameService.getAll();
        model.addAttribute("games",games);
        return "games/list-games";
    }


    @GetMapping("/new")
    public String displayProjectForm(Model model){

        Game aGame = new Game();
        List<Player> players = playerService.getAll();
        model.addAttribute("allPlayers",players);
        model.addAttribute("game",aGame);
        return "games/new-game";
    }


    @PostMapping("/save")
    public String createGame(Model model, @RequestParam List<Long> players, Game game){

        gameService.save(game);


        return "redirect:/games";
    }


}
